package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xumt.hwHMSdemo.Adapter.ChanceAddressAdapter;
import com.xumt.hwHMSdemo.Adapter.CommodityAdapter;
import com.xumt.hwHMSdemo.Adapter.ShoppingcarAdapter;
import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChanceAdress extends AppCompatActivity {
    private String result;
    private List<Map<String,Object>> addresslist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chance_adress);
        findaddress();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            JSONArray jsonArray = new JSONArray(result);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", jsonObject.getString("name"));
                map.put("phone",jsonObject.getString("phone"));
                map.put("address",jsonObject.getString("address"));
                map.put("moren",jsonObject.getString("moren"));
                addresslist.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.i("list","list"+addresslist);
        Log.i("list","list"+result);
        ChanceAddressAdapter chanceAddressAdapter=new ChanceAddressAdapter(this,addresslist);
        RecyclerView recyclerView= findViewById(R.id.addressRecyclerView);
//            recyclerView.addItemDecoration(new TopicRecycleViewDivider(this,R.drawable.bandline2));
//        StaggeredGridLayoutManager stagger = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(stagger);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(chanceAddressAdapter);
        Button button=(Button)findViewById(R.id.sunmitodrerbutton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Insertorder1(CommodityAdapter.allprice,chanceAddressAdapter.name,chanceAddressAdapter.phone,Newshoppingcar.Orderid,chanceAddressAdapter.Address);
                    Toast.makeText(ChanceAdress.this, "提交成功！" ,Toast.LENGTH_SHORT).show();
            }
        });

    }
    public void findaddress() {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    result = request.sendGet("http://169.254.132.31:8081/address/findaddress",
                            "username="+Login2.usernamen);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void Insertorder1(int price,String name,String phone,int orderid,String address)
    {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    result = request.sendGet("http://169.254.132.31:8081/Commdity/INSERTorder",
                            "price="+ price + "&" +"name=" + name + "&" + "phone=" + phone+"&" + "orderid=" + orderid+"&"+"address="+address);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}